//--------------------------------------------------------------
/// cGame_StateMachine.h
///
///
//--------------------------------------------------------------

// Macros to enable our State Structure
#define STATE_REGISTER( state )                     case state: game_handleMessage_##state(message); break
#define START_STATE_CODE( state )                   public void game_handleMessage_##state(int message) throws Exception
#define START_MESSAGE_HANDLER( msg_id )             if(message == msg_id)
#define SEND_MESSAGE( state, message )              game_handleMessage_##state( message )


// State vars
public static int s_iStateCurrent;
public static int s_iStatePrev;
public static int s_iStateNext;

public static boolean s_isCurrentStateExit;
public static boolean s_isNextStateEnter;

// Helper State vars
public static int s_globalFrameCounter;
public static int s_stateFrameCounter;
public static int s_globalStateTime;
public static long s_globalStateTimeReal;
public static int s_stateTime;

//-----------------------------------------------------------------------------
/// Initializes the State Machine Variables
//-----------------------------------------------------------------------------
public static void StateMachine_Init()
{
	s_iStateCurrent = STATE_INIT;
	// s_iStateCurrent = STATE_LOGO;
	s_iStatePrev	= -1;
	s_iStateNext	= -1;
	
	s_isCurrentStateExit 	= false;
	s_isNextStateEnter		= true;	
	
	s_globalFrameCounter	= 0;
	s_stateFrameCounter		= 0;
	s_globalStateTime		= 0;
	s_globalStateTimeReal	= 0;
	s_stateTime				= 0;
}

public static void SwitchState(int state)
{
	s_iStateNext			= state;
	s_isCurrentStateExit	= true;
	s_stateTime				= -1;
}
public void StateMachine_SendMessage(int message)
{
	try
	{
		switch(s_iStateCurrent)
		{
			STATE_REGISTER(STATE_INIT);
			STATE_REGISTER(STATE_LOGO);
			STATE_REGISTER(STATE_QUIT);
			STATE_REGISTER(STATE_SPLASHSCREEN);
			STATE_REGISTER(STATE_MAINMENU);
			STATE_REGISTER(STATE_SELECTLANGUAGE);
			// STATE_REGISTER(STATE_MAINLOADING);
			STATE_REGISTER(STATE_GAMEPLAY);
			STATE_REGISTER(STATE_IGM);
			STATE_REGISTER(STATE_MM_CONFIRM);
			STATE_REGISTER(STATE_EXIT_CONFIRM);
			// STATE_REGISTER(STATE_GAMEPAUSE);
			
			default:
				System.out.println("State ["+ s_iStateCurrent + "] is undefined. Message sent was: " + message);
			break;
		}
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
	}
}

public void StateMachine_Update() throws Exception
{
	if(s_isCurrentStateExit)
	{
		StateMachine_SendMessage(MESSAGE_EXIT);
		
		s_isCurrentStateExit 	= false;
		s_isNextStateEnter		= true;
		
		s_iStatePrev			= s_iStateCurrent;
		s_iStateCurrent			= s_iStateNext;
		s_iStateNext			= -1;
	}
	
	if(s_isNextStateEnter)
	{
		s_isNextStateEnter		= false;
		s_stateFrameCounter		= 0;
		s_stateTime				= 0;
		
		StateMachine_SendMessage(MESSAGE_ENTER);
	}
	
	StateMachine_SendMessage(MESSAGE_UPDATE);
	
	s_stateFrameCounter++;
	// s_stateTime++;
	
	s_globalFrameCounter++;
	s_globalFrameCounter &= 0xFFFF;
	
}

public void StateMachine_Paint() throws Exception
{
	StateMachine_SendMessage(MESSAGE_PAINT);
}